package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemRoles.*;
import com.softWang.system.po.SystemRoles;
import com.softWang.system.service.SystemRolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(value="角色管理",tags="角色管理")
@RequestMapping("/system/role")
public class SystemRolesController {
    @Resource
    private SystemRolesService rolesService;

    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @ApiOperation(value="角色列表(分页)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemRoleQueryAllResult>> queryall(@RequestBody @Validated SystemRoleQueryAllParam param, HttpServletRequest request) {
        List<SystemRoleQueryAllResult> results=rolesService.queryall(param);
        PageBean page=new PageBean(results);
        return ResponseResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @ApiOperation(value="添加角色")
    @SysOperationLog(value = "添加角色", model = SysModel.ROLE_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemRolesAddParam param, HttpServletRequest request) throws Exception {
        SystemRoles role = new SystemRoles();
        BeanUtils.copyProperties(param, role);
        Long id = rolesService.add(role);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @ApiOperation(value="修改角色")
    @SysOperationLog(value = "修改角色", model = SysModel.ROLE_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemRolesEditParam param, HttpServletRequest request) {
        SystemRoles role = new SystemRoles();
        BeanUtils.copyProperties(param, role);
        Long id = rolesService.edit(role);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:role:detail')")
    @ApiOperation(value="查看详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemRolesDetailResult> detail(@RequestBody @Validated Idparam idparam) {
        SystemRolesDetailResult result=new SystemRolesDetailResult();
        SystemRoles roles = rolesService.getById(idparam.getId());
        BeanUtils.copyProperties(roles, result);
        List<Long> menuIds = rolesService.getMenuIdsByRoleId(idparam.getId());
        result.setMenuIds(menuIds);
        List<Long> deptIds = rolesService.getDeptIdsByRoleId(idparam.getId());
        result.setDeptIds(deptIds);
        return ResponseResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('system:role:del')")
    @ApiOperation(value="删除")
    @SysOperationLog(value = "删除角色", model = SysModel.ROLE_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        rolesService.del(ids);
        return ResponseResult.success();
    }


    @ApiOperation(value = "可分配角色列表",notes = "可分配角色列表")
    @RequestMapping(value = "/getRoleList",method = RequestMethod.POST)
    public ResponseResult<List<SystemRoleGetRoleListResult>> getRoleList(HttpServletRequest request) {
        List<SystemRoleGetRoleListResult> list = rolesService.getRoleList();
        return ResponseResult.success(list);
    }

}
